package crawl.http;

import java.util.HashMap;
import java.util.Set;

/**
 * a web page, used to calculate the pageRank
 * 
 * @author huangcd
 * 
 */
public class Page implements Comparable<Page>
{
    public static double           minDiff;
    private String                 url;
    private double                 lastPageRank;
    private double                 currentPageRank;
    private int                    documentID;
    // nodes pointing to this page
    private HashMap<Page, Integer> inNode;
    // nodes pointing from this page
    private HashMap<Page, Integer> outNode;

    public Page(String name)
    {
        this.url = name;
        this.documentID = -1;
        inNode = new HashMap<Page, Integer>();
        outNode = new HashMap<Page, Integer>();
    }

    public void addInNode(Page node)
    {
        int times = inNode.containsKey(node) ? inNode.get(node) : 0;
        inNode.put(node, times + 1);
    }

    public void addOutNode(Page node)
    {
        int times = outNode.containsKey(node) ? outNode.get(node) : 0;
        outNode.put(node, times + 1);
    }

    /**
     * delete this page from the graph, first delete all the edge contains this
     * page, the delete this page
     */
    public void delete()
    {
        for (Page p : inNode.keySet())
        {
            p.outNode.remove(this);
        }
        for (Page p : outNode.keySet())
        {
            p.inNode.remove(this);
        }
        this.inNode.clear();
        this.outNode.clear();
        this.currentPageRank = 0;
        this.lastPageRank = 0;
    }

    public void setDocumentID(int id)
    {
        this.documentID = id;
    }

    public int getDocumentID()
    {
        return documentID;
    }

    public double getLastPageRank()
    {
        return lastPageRank;
    }

    public void setLastPageRank(double lastPageRank)
    {
        this.lastPageRank = lastPageRank;
    }

    public double getCurrentPageRank()
    {
        return currentPageRank;
    }

    public void setCurrentPageRank(double currentPageRank)
    {
        this.currentPageRank = currentPageRank;
    }

    public String getUrl()
    {
        return url;
    }

    public Set<Page> getInNodeList()
    {
        return inNode.keySet();
    }

    public Set<Page> getOutNodeList()
    {
        return outNode.keySet();
    }

    public int compareTo(Page o)
    {
        if (this.currentPageRank > o.currentPageRank)
            return -1;
        else if (this.currentPageRank < o.currentPageRank)
            return 1;
        else
            return 0;
    }
}
